 package mainClasses;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class InputAndOutput extends JPanel implements ActionListener{
	public static final int LABEL_AND_TEXT = 1;
	public static final int LABEL_AND_BUTTONS = 2;
	public static final int LABEL = 3;
	public static final int TWO_LABELS_AND_TEXT = 4;
	public static final int TEXTBOX = 5;
	private static final int COUNT_MAX = 2;
	public int mode = 3;


	private JTextField input;
	private JLabel output;

	private JPanel buttonPanel;
	public String string = " ";
	public String string_Two = " ";
	public String string_copy = " ";
	
	
	JTextArea textArea;
	JScrollPane scrollPane;
	private final static String NEWLINE = "\n";
	private int count = 1;
	
	BoardGame theGame;
	
	public InputAndOutput(){
		
		this.setLayout(new BorderLayout(2,1));
		this.output = new JLabel();
		this.output.setBackground(Color.black);

		this.input = new JTextField(20);
		this.buttonPanel = new JPanel();
		this.output.setOpaque(true);
		this.output.setFont(new Font("Time New Roman", Font.ITALIC, 15));
		this.output.setHorizontalAlignment(JTextField.CENTER);
		this.output.setAlignmentX(JTextField.CENTER);
		
		this.textArea = new JTextArea();
		this.textArea.setFont(new Font("Time New Roman", Font.ITALIC, 15));
		this.textArea.setAlignmentY(CENTER_ALIGNMENT);
		this.textArea.setEditable(false);
		this.scrollPane = new JScrollPane(textArea);
		this.scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		this.scrollPane.setPreferredSize(new Dimension(200, 200));
		this.scrollPane.setAutoscrolls(true);
	}
	
	
	
	
	
	public void setOutputField(String s)
	{
		if(this.count == 1 && !this.string.equals(s))
		{
			this.string_Two = " ";
			this.string_Two += "<p align=center><font color=white> " + this.string_copy + "</font></p>";
			this.string = " ";
			this.string_copy = s;
			this.string += "<p align=center><font color=red> " + s + "</font></p>";
		}else if(this.count == InputAndOutput.COUNT_MAX && !this.string.equals(s))
		{
			this.string_Two = " ";
			this.string_Two += "<p align=center><font color=white> " + this.string_copy + "</font></p>";
			this.string = " ";
			this.string_copy = s;
			this.string += "<p align=center><font color=red> " + s + "</font></p>";
			this.count = 1;
		}
		this.count++;
		this.output.setText("<html><font color=white> " + this.string_Two + " " + this.string + " </font><html>");
	}
	
	
	public JTextField getInputField()
	{
		return this.input;
	}
	
	public void clearHelpInfor()
	{
		this.textArea.setText(" ");
	}
	
	public void HelpInfor(String s)
	{
		this.textArea.append(s + NEWLINE);
	}
	
	
	public void actionPerformed(ActionEvent e)
	{
		//this.notifyAll();
	}
	
	
	
	
	public void setCurrentGame(BoardGame bg)
	{
		theGame = bg;
		this.input.addActionListener(theGame);
	}
	
	
	
	
	
	public void setMode(int mode)
	{
		this.mode = mode;
		this.removeAll();

		if(mode==LABEL_AND_TEXT)
		{
			this.add(output, BorderLayout.NORTH);
			this.add(input, BorderLayout.SOUTH);
		}
		
		else if(mode ==LABEL_AND_BUTTONS)
		{
			this.add(output, BorderLayout.NORTH);
			this.add(buttonPanel, BorderLayout.SOUTH);
		}
		
		else if(mode == LABEL)
		{
			this.add(output);
		}
		
		else if(mode == TWO_LABELS_AND_TEXT)
		{
			this.add(output, BorderLayout.CENTER);
			this.add(input, BorderLayout.SOUTH);
		}else if(mode==TEXTBOX)
		{
			this.add(this.scrollPane);
		}
	}
	
	
	public void addButtons(JButton allButtons[]){
		this.buttonPanel.removeAll();
		this.buttonPanel.setLayout(new GridLayout(1,allButtons.length));
		for(JButton button : allButtons){
			this.buttonPanel.add(button);

		}
	}

	
}
